package com.service;

import com.dao.BaseDao;
import com.entity.PageManager;
import com.entity.User;

import java.io.File;
import java.util.List;

public class UserService extends BaseDao {

    /*public Object[] getObjArr(User user){
        Object[] params = {user.getUsername(), user.getPassword(), user.getName(),
        user.getSex(), user.getAge(), user.getBirthday(), user.getPhone(), user.getHeight(),
        user.getPic()};
        return params;
    }*/

    /**
     * 增加用户数据
     * @param user
     * @return
     */
    public boolean addUser(User user) {
        /*String sql = "INSERT INTO `rz1217`.`user`(`username`, `password`, " +
                "`name`, `sex`, `age`, `birthday`, `phone`, " +
                "`height`, `pic`) " +
                "VALUES (?,?,?,?,?,?,?,?,?)";
        Object[] params = getObjArr(user);*/
        try {
            System.out.println(user);
            return insert(user) > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 分页查询普通用户
     * @param pm
     * @return
     */
    public List<User> queryUsers(PageManager pm) {
        int start = (pm.getPn() - 1) * pm.getRn();
        int end = pm.getRn();
        String sql = "select * from user where administrators = 'F' and Supertube = 'F' limit ?,?";
        return findList(User.class, sql, start, end);
    }

    /**
     * 分页查询管理员
     * @param pm
     * @return
     */
    public List<User> queryUsersVIP(PageManager pm) {
        int start = (pm.getPn() - 1) * pm.getRn();
        int end = pm.getRn();
        String sql = "select * from user where administrators = 'T' and Supertube = 'F' limit ?,?";
        return findList(User.class, sql, start, end);
    }


    /**
     * 分页查询
     * @return
     */
    public List<User> queryUsers() {
        String sql = "select * from user";
        return findList(User.class, sql);
    }

    public boolean delUser(int id) {
//      String sql = "delete from user where id = ?";
        User user = queryUser(id);
        boolean result = false;
        try {
            result = delete(new User(id))> 0;
            if (result) {
                if(user.getPic()!=null){
                    //删除图片
                    new File(user.getPic()).delete();
                }
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public User queryUser(int id) {
//        String sql = "select * from user where id = ?";
        try {
            return (User) query(new User(id));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean updateUser(User user) {
       /* String sql = "UPDATE `rz1217`.`user` SET `username` = ?, `password` = ?," +
                " `name` = ?, `sex` = ?, `age` = ?, `birthday` = ?, `phone` = ?, " +
                "`height` = ?, `pic` = ? WHERE `id` = ?";
        Object[] params = {user.getUsername(), user.getPassword(), user.getName(),
                user.getSex(), user.getAge(), user.getBirthday(), user.getPhone(), user.getHeight(),
                user.getPic(), user.getId()};*/
        //查询单条数据
        User queryUser = queryUser(user.getId());
        try {
            boolean result = update(user) > 0;
            //修改成功，并且传递的图片和数据库的图片不同时，才删除图片文件
            if (result && queryUser.getPic() != null
                && !queryUser.getPic().equals(user.getPic())) {
                //删除原图片
                new File(queryUser.getPic()).delete();
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public int queryUserTotalCount() {
        String sql = "select count(*) from user where administrators = 'F' and Supertube = 'F'";
        return count(sql);
    }
    public int queryUserTotalCountVTP() {
        String sql = "select count(*) from user where administrators = 'T' and Supertube = 'F'";
        return count(sql);
    }

    /**
     * 通过用户名查询用户数据
     * @param username 要查询的用户名
     * @return
     */
    public User queryUserByUsername(String username) {
        String sql = "select * from user where username = ?";
        return findBean(User.class, sql, username);
    }
}
